public class Main {
    public static int solution(int n, int[] a) {
        int maxValue = 0;
        for (int i = 0; i< n; i++) {
            for (int j = i+1; j < n; j++) {
                int dist = j - i;
                if (dist > n/2) {
                    dist = n - dist;
                }
                int currentValue = (a[i] + a[j]) * dist;
                if (currentValue > maxValue) {
                    maxValue = currentValue;
                }
            }
        }

        return maxValue;
    }

    public static void main(String[] args) {
        System.out.println(solution(3, new int[]{1, 2, 3}) == 5);
        System.out.println(solution(4, new int[]{4, 1, 2, 3}) == 12);
        System.out.println(solution(5, new int[]{1, 5, 3, 7, 2}) == 24);
    }
}